package com.voja.salonschedule.customers;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class RemoteImage extends AsyncTask<RemoteImage.RemoteImageParams, Integer, Bitmap>{
    
    public class RemoteImageParams {
        public Context context;
        public String url;
        
        public RemoteImageParams(Context context, String url) {
            this.context = context;
            this.url     = url;
        }
    };
    
    @Override
    protected Bitmap doInBackground(RemoteImage.RemoteImageParams... params) {
        Bitmap bm = null;
        if (params.length > 0) {
            String url      = params[0].url;
            try {
                bm = getRemoteImage(url);
            } catch (IOException e) {
                android.util.Log.e(this.getClass().getName(), e.getMessage());
            }
        }
        return bm;
    }
    
    private Bitmap getRemoteImage(String url) throws IOException {
        URL Url = new URL(url);
        URLConnection conn = Url.openConnection();
        BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
        Bitmap bm = BitmapFactory.decodeStream(in);
        in.close();
        
        return bm;
    }

}
